﻿@{
    Layout = "~/_SiteLayout.cshtml";

    string editID = Request.QueryString["id"];
    string name = string.Empty;
    string type = string.Empty;
    string connstr = string.Empty;
    string note = string.Empty;

    Business.Platform.DBConnection bdbConn = new Business.Platform.DBConnection();
    Data.Model.DBConnection dbConn = null;
    if(editID.IsGuid())
    {
        dbConn = bdbConn.Get(editID.ToGuid());
        if (dbConn != null && !IsPost)
        {
            name = dbConn.Name;
            connstr = dbConn.ConnectionString;
            type = dbConn.Type;
            note = dbConn.Note;
        }
    }
    
    if (IsPost)
    {
        AntiForgery.Validate();
        name = Request.Form["Name"];
        connstr = Request.Form["ConnStr"];
        type = Request.Form["LinkType"];
        note = Request.Form["Note"];
        if (name.IsNullOrEmpty() || connstr.IsNullOrEmpty() || type.IsNullOrEmpty())
        {
            <script type="text/javascript">
                alert("数据验证错误!");
            </script>
        }
        else
        {
            bool isAdd = dbConn == null;
            string oldXML = dbConn.Serialize();
            if (isAdd)
            {
                isAdd = true;
                dbConn = new Data.Model.DBConnection();
                dbConn.ID = Guid.NewGuid();
            }

            dbConn.Name = name.Trim();
            dbConn.Type = type;
            dbConn.Note = note.IsNullOrEmpty() ? null : note.Trim();
            dbConn.ConnectionString = connstr.Trim();
            string url = "/Platform/DBConn/Default" + Request.Url.Query;
            if (isAdd)
            {
                bdbConn.Add(dbConn);
                Business.Platform.Log.Add("添加了数据连接", dbConn.Serialize(), Business.Platform.Log.Types.流程相关);
                <script type="text/javascript">
                    new RoadUI.Window().reloadOpener('@url');
                    alert("添加成功!");
                </script>
            }
            else
            {
                bdbConn.Update(dbConn);
                Business.Platform.Log.Add("修改了数据连接", string.Format("修改前:{0}修改后:{1}", oldXML, dbConn.Serialize()),
                    Business.Platform.Log.Types.流程相关);
                <script type="text/javascript">
                    new RoadUI.Window().reloadOpener('@url');
                    alert("保存成功!");
                </script>
            }
            bdbConn.ClearCache();
        }
    }

    string typeOptions = bdbConn.GetAllTypeOptions(type);
}
<form method="post" action="" onsubmit="return new RoadUI.Validate().validateForm(this);">
@AntiForgery.GetHtml()
<br />
    <table cellpadding="0" cellspacing="1" border="0" width="99%" class="formtable">
    <tr>
        <th style="width: 80px;">
            连接名称：
        </th>
        <td>
            <input type="text" name="Name" id="Name" class="mytext" value="@name" validate="empty" style="width: 75%" />
        </td>
    </tr>
    <tr>
        <th>
            连接类型：
        </th>
        <td>
            <select id="LinkType" class="myselect" name="LinkType"><option value=""></option>@Html.Raw(typeOptions)</select>
        </td>
    </tr>
    <tr>
        <th>
            连接字符串：
        </th>
        <td>
            <textarea class="mytext" name="ConnStr" cols="1" rows="1" style="width: 95%; height: 50px;">@connstr</textarea>
        </td>
    </tr>
    <tr>
        <th>
            备注：
        </th>
        <td>
            <textarea class="mytext" name="Note" cols="1" rows="1" style="width: 95%; height: 50px;">@note</textarea>
        </td>
    </tr>
    </table>
    <div class="buttondiv">
        <input type="submit" value="确定保存" class="mybutton" />
        <input type="button" class="mybutton" value="取消关闭" style="margin-left: 5px;" onclick="closewin();" />
    </div>
    <script type="text/javascript">
        var win = new RoadUI.Window();
        $(function ()
        {
            
        });
        function closewin()
        {
            win.close();
            return false;
        }
    </script>
</form>